package cn.ffcs.businessrequest.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.bpm.dao.ProcessConfigDAO;
import cn.ffcs.bpm.dao.TransactProcessStatusDAO;
import cn.ffcs.bpm.dao.TransactorDAO;
import cn.ffcs.bpm.service.BizProcessManageService;
import cn.ffcs.businessrequest.dao.BusinessRequestDAO;
import cn.ffcs.businessrequest.dao.IDocContentDAO;
import cn.ffcs.businessrequest.dao.IReqDelayDAO;
import cn.ffcs.businessrequest.dao.IReqDocuDAO;
import cn.ffcs.businessrequest.dao.IReqProAttachDAO;
import cn.ffcs.businessrequest.dao.IReqProgressDAO;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.DocContent;
import cn.ffcs.businessrequest.model.ReqDelay;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.businessrequest.model.ReqProgAttach;
import cn.ffcs.businessrequest.model.ReqProgress;
import cn.ffcs.base.service.BaseService;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.dao.IAttachDAO;
import cn.ffcs.common.model.Attach;

/**
 * 业务需求交互单服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author huangjj
 * @version $Id$
 */
public class ReqProgressService<D extends ReqProgress> extends
        BizProcessManageService<D>  implements IReqProgressService<D>
{
    private static final int BUFFER_SIZE = 512 * 1024;

    /**
     * 进展情况DAO
     */
    private IReqProgressDAO<ReqProgress> reqProgressDAO;
    
    /**
     * 延迟申请DAO
     */
    private IReqDelayDAO<ReqDelay> reqDelayDAO;
    
    /**
     * 附件DAO
     */
    private IReqProAttachDAO<ReqProgAttach> reqProAttachDAO;

    private BusinessRequestDAO businessRequestDAO;

    /**
     * 按查询条件获取总数
     * 重写
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<D> findBy(Criterion cond)
    {
        return (List<D>) reqProgressDAO.findBy(cond);
    }
    

    /**
     * 按查询条件获取总数
     * 重写
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<ReqDelay> findByrd(Criterion cond)
    {
        return (List<ReqDelay>) reqDelayDAO.findBy(cond);
    }
    
    /**
     * 按查询条件获取总数
     * 重写
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<ReqProgAttach> findAttachBy(Criterion cond)
    {
        return (List<ReqProgAttach>) reqProAttachDAO.findBy(cond);
    }
    

    /**
     * 修改延迟信息操作
     */
    @SuppressWarnings("unchecked")
    public void updateReqDelay(ReqDelay reqDelay)
    {
         reqDelayDAO.update(reqDelay);
    }
    

    /**
     * 修改进展信息操作
     */
    @SuppressWarnings("unchecked")
    public void updateReqProgress(ReqProgress reqProgress)
    {
        reqProgressDAO.update(reqProgress);
    }

    /**
     * 保存需求延迟信息操作
     */
    @SuppressWarnings("unchecked")
    public boolean saveReqDelay(ReqDelay reqDelay)
    {

        // 保存进展信息数据

        try
        {
        reqDelayDAO.save(reqDelay);
        
        /** 保存附件
        if (uploads != null)
        {
            int i = 0;
            for (i = 0; i < uploads.size(); i++)
            {
                    File file = uploads.get(i);
                    byte[] result = new byte[BUFFER_SIZE];
                    result = FileUtils.readFileToByteArray(file);
                    FileUtils.forceDelete(file);

//                    String fileName = fileNames.get(i);
//                    String fullName = fileName;
//                    String name = fullName.substring(0, fullName.indexOf("."));
//                    String prefix = fullName.substring(
//                            fullName.indexOf(".") + 1, fullName.length());

                    ReqProgAttach reqProgAttach=new ReqProgAttach();
                    reqProgAttach.setReqProgress(reqDelay);
                    reqProgAttach.setContent(result);
                    //保存附件信息
                    reqProAttachDAO.save(reqProgAttach);

            }
        }**/
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    
    /**
     * 保存需求进展信息操作
     */
    @SuppressWarnings("unchecked")
    public boolean saveReqProgress(D reqProgress, List<File> uploads)
    {

        // 保存进展信息数据

        try
        {
        reqProgressDAO.save(reqProgress);
        
        // 保存附件
        if (uploads != null)
        {
            int i = 0;
            for (i = 0; i < uploads.size(); i++)
            {
                    File file = uploads.get(i);
                    byte[] result = new byte[BUFFER_SIZE];
                    result = FileUtils.readFileToByteArray(file);
                    FileUtils.forceDelete(file);

//                    String fileName = fileNames.get(i);
//                    String fullName = fileName;
//                    String name = fullName.substring(0, fullName.indexOf("."));
//                    String prefix = fullName.substring(
//                            fullName.indexOf(".") + 1, fullName.length());

                    ReqProgAttach reqProgAttach=new ReqProgAttach();
                    reqProgAttach.setReqProgress(reqProgress);
                    reqProgAttach.setContent(result);
                    //保存附件信息
                    reqProAttachDAO.save(reqProgAttach);

            }
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    
    /**
     * 删除操作
     * @param baseEntity
     */
    public void remove(D businessRequest)
    {
//        if (businessRequest != null)
//        {
//            Criterion cond1 = Restrictions.eq("refEntity", businessRequest
//                    .getClass().getName());
//            Criterion cond2 = Restrictions.eq("refEntityId", businessRequest
//                    .getId().toString());
//            Criterion condAttach = Restrictions.and(cond1, cond2);
//            List<ReqDocu> reqDocus = reqDocuDAO.findBy(condAttach);
//            for (ReqDocu att : reqDocus)
//            {
//                reqDocuDAO.deleteById(att.getId() + "");
//            }
//            super.remove(businessRequest);
//        }
    }

    public BusinessRequestDAO getBusinessRequestDAO()
    {
        return businessRequestDAO;
    }

    public void setBusinessRequestDAO(BusinessRequestDAO businessRequestDAO)
    {
        this.businessRequestDAO = businessRequestDAO;
    }

    public IReqProgressDAO<ReqProgress> getReqProgressDAO()
    {
        return reqProgressDAO;
    }

    public void setReqProgressDAO(IReqProgressDAO<ReqProgress> reqProgressDAO)
    {
        this.reqProgressDAO = reqProgressDAO;
    }

    public IReqProAttachDAO<ReqProgAttach> getReqProAttachDAO()
    {
        return reqProAttachDAO;
    }

    public void setReqProAttachDAO(IReqProAttachDAO<ReqProgAttach> reqProAttachDAO)
    {
        this.reqProAttachDAO = reqProAttachDAO;
    }

    public IReqDelayDAO<ReqDelay> getReqDelayDAO()
    {
        return reqDelayDAO;
    }

    public void setReqDelayDAO(IReqDelayDAO<ReqDelay> reqDelayDAO)
    {
        this.reqDelayDAO = reqDelayDAO;
    }

}
